if "导入sys.path":
    import sys

    # sys.path.append(r"F:\A_RuanJian\Py_project\pro_test\Quicker")
    sys.path.append(r"F:\A_RuanJian\Py_project\pro_test")
    # path.append 一定要放在开头
if "导入其他":
    from click import command, option
    from flask import Flask, request, jsonify
    import logging
    from Quicker.qk_python import *
    from Quicker.qk_comple import AutoComple
    from base.util import proc_search

    # 不打印普通消息
    log = logging.getLogger('werkzeug')
    log.setLevel(logging.ERROR)
    # log.setLevel(logging.INFO)
    app = Flask(__name__)


@app.route('/post', methods=['POST'])
def post():
    # request_method = request.method
    # if request_method == 'POST':
    if "获取参数":
        keyCode = request.form.get("keyCode")
        operate = request.form.get("operate")
        name = request.form.get("name")
        val = request.form.get("val")
        version = request.form.get("version")
        text = request.form.get("text")
        code = request.form.get("code") or request.form.get("exec")
        vars_q = request.form.get("vars_q")
        OriList: str = request.form.get("OriList")
        procName = request.form.get("procName")

    if operate:  # 全局变量管理
        val = json.loads(val)  # 因为val是动态类型, 所以需要反序列化
        _ = jsonify(globals_operate(operate, name, val))
        # pp(type_=1)(_)
        return _
    if OriList:  # 补全#
        # cc(procName)
        self_ = AutoComple(OriList, procName)
        resp = self_.run()
        # pp()(OriList, procName, resp)
        return jsonify(resp)
    if keyCode:  # 对符
        return jsonify(keyCodeCheck(keyCode)())
    if code:  # 执行代码
        res = CodeExec().main(code, version, vars_q)
        # print(res)
        return jsonify(res)

    if text:
        # 测试用
        return jsonify(text)


@command()
@option('-port', default=5002, help='')  # 测试版
def run(**kwargs):
    # 防止端口冲突
    port = kwargs["port"]
    proc_search(port, killall=1)
    # 实现持久连接, 但也多了更多验证规则。
    # WSGIRequestHandler.protocol_version = 'HTTP/1.1'
    app.config['JSON_AS_ASCII'] = 0  # unicode显示为中文
    app.run(debug=0, host='127.0.0.1', port=port)


"""
访问主页：
127.0.0.1:5000
直接访问路由：
127.0.0.1:5000/get
"""
if __name__ == "__main__":
    # 说明
    # cmd: python [path] -port 5001
    # 为什么不分开两个文件? 只能用同一个文件?
    #   因为执行代码的时候还是在父类的exec模块中, 无法获取到子类的模块
    # 如果出现错误, 有可能是端口被抢占
    run()
    # port = 5002
    # proc_search(port, killall=1)
    # _ = proc_search(500, name="", fuzzy=1)
    # proc_search(port, killall=1)
    # f = lambda e: [Proc.name(e), Proc.get_port(e)]
    # _ = map(f, _)
    # pp()(_)
    # app.config['JSON_AS_ASCII'] = 0  # unicode显示为中文
    # app.run(debug=0, host='127.0.0.1', port=port)
